package com.playball.lottery.fragment.frm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.playball.lottery.R;
import com.playball.lottery.customview.ShowTipDialog;
import com.playball.lottery.eventmanger.EventManager;
import com.playball.lottery.eventmanger.EventType;
import com.playball.lottery.frm.help.CaiPiaoHelp;
import com.playball.lottery.frm.hemai.HemaiMainProcessListAdapter;
import com.playball.lottery.nt.GetRequestParams;
import com.playball.lottery.sessions.SessionManager;
import com.playball.lottery.util.ConvertJsonToList;
import com.playball.lottery.util.LotConstants;
import com.playball.lottery.widget.PullToRefresh.PullToRefreshListView;
import com.playball.lottery.widget.PullToRefresh.PullToRefreshBase.OnRefreshListener;


public class HeMaiMainFragment extends Fragment implements LotConstants {
    public static final String TAG = "HeMaiMainFragment";

    View _RootView;

    /** 顶部菜单栏 */
    private TextView top_title_back;
    private TextView top_title_right_btn;
    /** 进度排序相关 */
    private View hemai_main_ll_progress;
    private TextView hemai_main_tv_progress;
    private TextView hemai_main_tv_progress_order_0;
    private TextView hemai_main_tv_progress_order_1;
    /** 战绩排序相关 */
    private View hemai_main_ll_grade;
    private TextView hemai_main_tv_grade;
    private TextView hemai_main_tv_grade_order_0;
    private TextView hemai_main_tv_grade_order_1;
    /** 总额排序相关 */
    private View hemai_main_ll_total;
    private TextView hemai_main_tv_total;
    private TextView hemai_main_tv_total_order_0;
    private TextView hemai_main_tv_total_order_1;
    /** 刷新按钮 */
    private ImageButton hemai_main_ib_refresh;
    /** 加载层 */
    // private View hemai_lv_loading;
    private View hemai_lv_tip;
    private TextView hemai_lv_tip_tv;

    private PullToRefreshListView hemai_ptrl_process;
    private ListView mListView;

    /** 排序彩种 */
    private int LottID = 0;
    /** 用于排序的列名（3=进度， 1=总额 ，5=战绩） */
    private int OrderField = 0;
    /** 排序方式（0：倒序；1：正序） */
    private int OrderType = 0;
    /** 第几页 */
    private int PageNo = 1;
    /** 每页条数 */
    private int PageSize = 10;

    /** 记录总条数 */
    private int intTotalNum;
    /** 网络获取的数据 */
    private List<Map<String, Object>> netDataList;
    /** 是否有下一页 */
    private boolean nextPage = false;
    /** 是否按更多 */
    private boolean isPressMore = false;
    /** 填充到listview的本地数据 */
    private List<Map<String, Object>> mDataList;
    /** 合买大厅适配器 */
    private HemaiMainProcessListAdapter hemaiAdapter;

    /** TOP */
    private View hemai_lv_go_top;

    /** 菜单弹出框 */
    private PopupWindow menu_popupWindow;
    /** 菜单弹出框 */
    private View menutypeview;

    private String strLot = "合买大厅";
    /** 当前显示玩法ID 默认足球90,全部0 */
    private int lotID = 0;// 正式改为90,0做测试用

    // Session Manager Class
    private SessionManager session;
    private Map<String, String> userDetails;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Session Manager
        session = new SessionManager(getActivity().getApplicationContext());
        if (session.isLoggedIn()) {
            userDetails = session.getUserDetails();
        } else {
            userDetails = new HashMap<>();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        _RootView = inflater.inflate(R.layout.hemai_main2, null);
        return _RootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        top_title_back = (TextView) _RootView.findViewById(R.id.top_title_back);

        final RelativeLayout hemai_top_title = (RelativeLayout) _RootView.findViewById(R.id.hemai_top_title);
        hemai_top_title.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                LayoutInflater inflater = LayoutInflater.from(getActivity());
                menutypeview = inflater.inflate(R.layout.hemai_topmenulink, null);
                menu_popupWindow = new PopupWindow(menutypeview, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                menu_popupWindow.setBackgroundDrawable(new BitmapDrawable());

                menu_popupWindow.setFocusable(true);
                menu_popupWindow.setTouchable(true);
                menu_popupWindow.setOutsideTouchable(false);

                menu_popupWindow.showAtLocation(menutypeview, Gravity.CENTER | Gravity.TOP, 0, 0);
                menu_popupWindow.showAsDropDown(hemai_top_title);
                menutypeview.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        menu_popupWindow.dismiss();
                    }
                });
                setBtnOnClickTopMenu(R.id.hemai_topmenulink_Button00);
                setBtnOnClickTopMenu(R.id.hemai_topmenulink_Button01);
                setBtnOnClickTopMenu(R.id.hemai_topmenulink_Button02);
                setBtnOnClickTopMenu(R.id.hemai_topmenulink_Button03);
                setBtnOnClickTopMenu(R.id.hemai_topmenulink_Button04);
                setBtnOnClickTopMenu(R.id.hemai_topmenulink_Button05);
                setBtnOnClickTopMenu(R.id.hemai_topmenulink_Button06);
                setBtnOnClickTopMenu(R.id.hemai_topmenulink_Button07);
                setBtnOnClickTopMenu(R.id.hemai_topmenulink_Button08);
                setBtnOnClickTopMenu(R.id.hemai_topmenulink_Button09);
                setBtnOnClickTopMenu(R.id.hemai_topmenulink_Button10);
                setBtnOnClickTopMenu(R.id.hemai_topmenulink_Button11);
                setBtnOnClickTopMenu(R.id.hemai_topmenulink_Button12);
                // select first button
                if (lotID == 0) {
                    ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button00)).setSelected(true);
                } else if (lotID == 1) {
                    ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button01)).setSelected(true);
                } else if (lotID == 3) {
                    ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button02)).setSelected(true);
                } else if (lotID == 5) {
                    ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button03)).setSelected(true);
                } else if (lotID == 6) {
                    ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button04)).setSelected(true);
                } else if (lotID == 13) {
                    ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button05)).setSelected(true);
                } else if (lotID == 19) {
                    ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button06)).setSelected(true);
                } else if (lotID == 39) {
                    ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button07)).setSelected(true);
                } else if (lotID == 63) {
                    ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button08)).setSelected(true);
                } else if (lotID == 64) {
                    ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button09)).setSelected(true);
                } else if (lotID == 45) {
                    ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button10)).setSelected(true);
                } else if (lotID == 90) {
                    ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button11)).setSelected(true);
                } else if (lotID == 91) {
                    ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button12)).setSelected(true);
                }
            }
        });

        top_title_right_btn = (TextView) _RootView.findViewById(R.id.top_title_right_btn);

        hemai_main_ll_progress = _RootView.findViewById(R.id.hemai_main_ll_progress);
        hemai_main_tv_progress = (TextView) _RootView.findViewById(R.id.hemai_main_tv_progress);
        hemai_main_tv_progress_order_0 = (TextView) _RootView.findViewById(R.id.hemai_main_tv_progress_order_0);
        hemai_main_tv_progress_order_1 = (TextView) _RootView.findViewById(R.id.hemai_main_tv_progress_order_1);

        hemai_main_ll_grade = _RootView.findViewById(R.id.hemai_main_ll_grade);
        hemai_main_tv_grade = (TextView) _RootView.findViewById(R.id.hemai_main_tv_grade);
        hemai_main_tv_grade_order_0 = (TextView) _RootView.findViewById(R.id.hemai_main_tv_grade_order_0);
        hemai_main_tv_grade_order_1 = (TextView) _RootView.findViewById(R.id.hemai_main_tv_grade_order_1);

        hemai_main_ll_total = _RootView.findViewById(R.id.hemai_main_ll_total);
        hemai_main_tv_total = (TextView) _RootView.findViewById(R.id.hemai_main_tv_total);
        hemai_main_tv_total_order_0 = (TextView) _RootView.findViewById(R.id.hemai_main_tv_total_order_0);
        hemai_main_tv_total_order_1 = (TextView) _RootView.findViewById(R.id.hemai_main_tv_total_order_1);

        hemai_main_ib_refresh = (ImageButton) _RootView.findViewById(R.id.hemai_main_ib_refresh);

        hemai_ptrl_process = (PullToRefreshListView) _RootView.findViewById(R.id.hemai_ptrl_process);
        mListView = hemai_ptrl_process.getRefreshableView();

        hemai_lv_tip = _RootView.findViewById(R.id.hemai_lv_tip);
        hemai_lv_tip_tv = (TextView) _RootView.findViewById(R.id.hemai_lv_tip_tv);

        hemai_lv_go_top = _RootView.findViewById(R.id.hemai_lv_go_top);

        top_title_back.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                EventManager.getInstance().onEvent(EventType.EV_UI_BLL_SCROLL_TO_LEFT, null);
            }
        });
        top_title_right_btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(
                        getActivity(),
                        CaiPiaoHelp.class);
                intent.putExtra("cp_title", "合买");
                intent.putExtra("cp_url", "hmbz.html");
                startActivity(intent);


            }
        });
        hemai_main_ll_progress.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                sort_click(3);
            }
        });
        hemai_main_ll_grade.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                sort_click(5);
            }
        });
        hemai_main_ll_total.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                sort_click(1);
            }
        });
        hemai_main_ib_refresh.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                onRefresh();
            }
        });

        hemai_ptrl_process.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                if(hemai_ptrl_process.getRefreshType() == 1) { //下拉
                    PageNo = 1;
                    startHBuyThread(lotID, PageNo);
                } else if(hemai_ptrl_process.getRefreshType() == 2) { //上拉
                    isPressMore = true;
                    // 发送更多请求
                    PageNo++;
                    startHBuyThread(lotID, PageNo);
                }
            }
        });

        hemai_lv_go_top.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    hemai_ptrl_process.requestFocusFromTouch();
                    hemai_ptrl_process.setSelection(0);
                } catch (Exception e) {
                    // e.printStackTrace();
                }
            }
        });


        hemai_ptrl_process.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem < 2) {
                    hemai_lv_go_top.setVisibility(View.GONE);
                } else {
                    hemai_lv_go_top.setVisibility(View.VISIBLE);
                }
            }
        } );


        top_title_right_btn.setVisibility(View.VISIBLE);
        top_title_right_btn.setText("帮助");

    }

    /**
     * 菜单玩法按钮事件
     *
     * @param btnId
     */
    protected void setBtnOnClickTopMenu(final int btnId) {
        Button btn = (Button) menutypeview.findViewById(btnId);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    menu_popupWindow.dismiss();
                    setOrderFieldMenu(btnId);
                    // 发送不同彩种网络请求
                    TextView top_title_tvplaytype = (TextView) _RootView.findViewById(R.id.top_title_tvhemai);
                    top_title_tvplaytype.setText(strLot);
                    // reset();

                } catch (Exception e) {
                    // e.printStackTrace();
                }
            }
        });

    }

    /**
     * 获取玩法及改变按钮背景
     *
     * @param btnId
     */
    protected void setOrderFieldMenu(int btnId) {
        // 恢复默认
        ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button00)).setSelected(false);
        ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button01)).setSelected(false);
        ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button02)).setSelected(false);
        ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button03)).setSelected(false);
        ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button04)).setSelected(false);
        ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button05)).setSelected(false);
        ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button06)).setSelected(false);
        ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button07)).setSelected(false);
        ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button08)).setSelected(false);
        ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button09)).setSelected(false);
        ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button10)).setSelected(false);
        ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button11)).setSelected(false);
        ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button12)).setSelected(false);
        switch (btnId) {
            case R.id.hemai_topmenulink_Button00:
                lotID = 0;
                strLot = "合买大厅";
                PageNo = 1;
                ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button00)).setSelected(true);
                if (mDataList != null) {
                    mDataList.removeAll(mDataList);
                    // mDataList.clear();
                }
                startHBuyThread(lotID, PageNo);
                break;
            case R.id.hemai_topmenulink_Button01:
                lotID = 1;
                strLot = "胜负彩";
                ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button01)).setSelected(true);
                PageNo = 1;
                if (mDataList != null) {
                    mDataList.removeAll(mDataList);
                    // mDataList.clear();
                }
                startHBuyThread(lotID, PageNo);
                break;
            case R.id.hemai_topmenulink_Button02:
                lotID = 3;
                strLot = "七星彩";
                ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button02)).setSelected(true);
                PageNo = 1;
                if (mDataList != null) {
                    mDataList.removeAll(mDataList);
                    // mDataList.clear();
                }
                startHBuyThread(lotID, PageNo);
                break;
            case R.id.hemai_topmenulink_Button03:
                lotID = 5;
                strLot = "双色球";
                ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button03)).setSelected(true);
                PageNo = 1;
                startHBuyThread(lotID, PageNo);
                break;
            case R.id.hemai_topmenulink_Button04:
                lotID = 6;
                strLot = "福彩3D";
                PageNo = 1;
                ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button04)).setSelected(true);
                startHBuyThread(lotID, PageNo);
                break;
            case R.id.hemai_topmenulink_Button05:
                lotID = 13;
                strLot = "七乐彩";
                PageNo = 1;
                ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button05)).setSelected(true);
                startHBuyThread(lotID, PageNo);
                break;
            case R.id.hemai_topmenulink_Button06:
                lotID = 19;
                strLot = "任选九";
                PageNo = 1;
                ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button06)).setSelected(true);
                startHBuyThread(lotID, PageNo);
                break;
            case R.id.hemai_topmenulink_Button07:
                lotID = 39;
                strLot = "大乐透";
                PageNo = 1;
                ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button07)).setSelected(true);
                startHBuyThread(lotID, PageNo);
                break;
            case R.id.hemai_topmenulink_Button08:
                lotID = 63;
                strLot = "排列3";
                PageNo = 1;
                ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button08)).setSelected(true);
                startHBuyThread(lotID, PageNo);
                break;
            case R.id.hemai_topmenulink_Button09:
                lotID = 64;
                strLot = "排列5";
                PageNo = 1;
                ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button09)).setSelected(true);
                startHBuyThread(lotID, PageNo);
                break;
            case R.id.hemai_topmenulink_Button10:
                lotID = 45;
                strLot = "北京单场";
                PageNo = 1;
                ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button10)).setSelected(true);
                startHBuyThread(lotID, PageNo);
                break;
            case R.id.hemai_topmenulink_Button11:
                lotID = 90;
                strLot = "竞彩足球";
                PageNo = 1;
                ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button11)).setSelected(true);
                startHBuyThread(lotID, PageNo);
                break;
            case R.id.hemai_topmenulink_Button12:
                lotID = 91;
                strLot = "竞彩篮球";
                PageNo = 1;
                ((Button) menutypeview.findViewById(R.id.hemai_topmenulink_Button12)).setSelected(true);
                startHBuyThread(lotID, PageNo);
                break;
        }

    }

    public void onResume() {
        super.onResume();
        onRefresh();
    }
    public void onPause() {
        super.onPause();
    }

    /**
     *
     * @param orderField2
     *            排序列名
     */
    private void sort_click(int orderField2) {
        // hemai_lv_loading.setVisibility(View.VISIBLE);
        switch (orderField2) {
            case 3: // 进度
                if (orderField2 == OrderField) {
                    OrderType = (hemai_main_tv_progress_order_0.getVisibility() == View.GONE ? 0 : 1);
                } else {
                    OrderField = orderField2;
                    OrderType = (hemai_main_tv_progress_order_0.getVisibility() == View.GONE ? 1 : 0);
                }
                hemai_main_ll_progress.setSelected(true);
                hemai_main_tv_progress.setSelected(true);
                hemai_main_tv_progress_order_0.setVisibility(View.GONE);
                hemai_main_tv_progress_order_1.setVisibility(View.GONE);
                if (OrderType == 0) {
                    hemai_main_tv_progress_order_0.setVisibility(View.VISIBLE);
                    hemai_main_tv_progress_order_0.setSelected(true);
                } else {
                    hemai_main_tv_progress_order_1.setVisibility(View.VISIBLE);
                    hemai_main_tv_progress_order_1.setSelected(true);
                }
                hemai_main_ll_grade.setSelected(false);
                hemai_main_tv_grade.setSelected(false);
                hemai_main_tv_grade_order_0.setSelected(false);
                hemai_main_tv_grade_order_1.setSelected(false);
                hemai_main_ll_total.setSelected(false);
                hemai_main_tv_total.setSelected(false);
                hemai_main_tv_total_order_0.setSelected(false);
                hemai_main_tv_total_order_1.setSelected(false);
                break;
            case 5: // 战绩
                if (orderField2 == OrderField) {
                    OrderType = (hemai_main_tv_grade_order_0.getVisibility() == View.GONE ? 0 : 1);
                } else {
                    OrderField = orderField2;
                    OrderType = (hemai_main_tv_grade_order_0.getVisibility() == View.GONE ? 1 : 0);
                }
                hemai_main_ll_progress.setSelected(false);
                hemai_main_tv_progress.setSelected(false);
                hemai_main_tv_progress_order_0.setSelected(false);
                hemai_main_tv_progress_order_1.setSelected(false);
                hemai_main_ll_grade.setSelected(true);
                hemai_main_tv_grade.setSelected(true);
                hemai_main_tv_grade_order_0.setVisibility(View.GONE);
                hemai_main_tv_grade_order_1.setVisibility(View.GONE);
                if (OrderType == 0) {
                    hemai_main_tv_grade_order_0.setVisibility(View.VISIBLE);
                    hemai_main_tv_grade_order_0.setSelected(true);
                } else {
                    hemai_main_tv_grade_order_1.setVisibility(View.VISIBLE);
                    hemai_main_tv_grade_order_1.setSelected(true);
                }
                hemai_main_ll_total.setSelected(false);
                hemai_main_tv_total.setSelected(false);
                hemai_main_tv_total_order_0.setSelected(false);
                hemai_main_tv_total_order_1.setSelected(false);
                break;
            case 1: // 总额
                if (orderField2 == OrderField) {
                    OrderType = (hemai_main_tv_total_order_0.getVisibility() == View.GONE ? 0 : 1);
                } else {
                    OrderField = orderField2;
                    OrderType = (hemai_main_tv_total_order_0.getVisibility() == View.GONE ? 1 : 0);
                }
                hemai_main_ll_progress.setSelected(false);
                hemai_main_tv_progress.setSelected(false);
                hemai_main_tv_progress_order_0.setSelected(false);
                hemai_main_tv_progress_order_1.setSelected(false);
                hemai_main_ll_grade.setSelected(false);
                hemai_main_tv_grade.setSelected(false);
                hemai_main_tv_grade_order_0.setSelected(false);
                hemai_main_tv_grade_order_1.setSelected(false);
                hemai_main_ll_total.setSelected(true);
                hemai_main_tv_total.setSelected(true);
                hemai_main_tv_total_order_0.setVisibility(View.GONE);
                hemai_main_tv_total_order_1.setVisibility(View.GONE);
                if (OrderType == 0) {
                    hemai_main_tv_total_order_0.setVisibility(View.VISIBLE);
                    hemai_main_tv_total_order_0.setSelected(true);
                } else {
                    hemai_main_tv_total_order_1.setVisibility(View.VISIBLE);
                    hemai_main_tv_total_order_1.setSelected(true);
                }
                break;
        }
        PageNo = 1;
        startHBuyThread(lotID, PageNo);
    }

//    /**
//     * 从服务器获取合买列表网络数据
//     *
//     * @param strLotId彩种
//     *            所有=0，双色球=5,超级大乐透=39，其他数字彩=-1. etc.
//     * @param orderType
//     *            排序方式（0：倒序；1：正序）
//     * @param orderField
//     *            3=进度， 1=总额 ，5=战绩
//     * @param pageNo
//     *            第几页
//     */
    private void startHBuyThread(int lottID, int page) {

        AsyncLoader task = new AsyncLoader();
        String[] dyn = new String[2];
        JSONObject params = new JSONObject();

        try {
            // 设置请求所需参数
            params.put("LottID", lottID);
            params.put("OrderField", OrderField);
            params.put("OrderType", OrderType);
            params.put("PageNo", page);
            params.put("PageSize", 10);
            params.put("LSID", userDetails.get(session.KEY_LSID));
        } catch (JSONException ex) {
            // TODO Auto-generated catch block
            Log.e("error", ex.toString());
        }
        dyn[0] = params.toString();
        dyn[1] = "206";// 212//合买中心方案列表
        // Log.e("startHBuyThread= ", dyn[1].toString() + " " +
        // dyn[0].toString());
        Log.d("van", dyn[0] + "---" + dyn[1]);
        task.execute(dyn);
    }

    /**
     * 接受处理网络数据
     *
     * @author Administrator
     *
     */
    class AsyncLoader extends AsyncTask<String, Integer, String> {// 继承AsyncTask

        String action;
        ShowTipDialog showdialog = new ShowTipDialog();

        // 1.准备运行
        protected void onPreExecute() {
            hemai_lv_tip.setVisibility(View.GONE);
            showdialog.showDialog(getActivity());
        }
        @Override
        protected String doInBackground(String... paramsArr) {
            String strResult = null;
            action = paramsArr[1];
            strResult = GetRequestParams.getRequestResult(paramsArr[0], action, getActivity());
            return strResult;
        }
        protected void onProgressUpdate(Integer... progress) {
        }
        protected void onPostExecute(String result) {
            showdialog.closeDialog();
            if (result != null) {
                if (action.equals("206")) {
                    HBuy(result);
                }
            } else {
                Toast.makeText(getActivity(), getActivity().getString(R.string.problem_01), Toast.LENGTH_LONG).show();
            }
            hemai_ptrl_process.onRefreshComplete();
        }
        protected void onCancelled() {
        }

    }

    /**
     * 处理网络返回数据及显示listview视图
     *
     * @param strResult
     */
    public void HBuy(String strResult) {
        try {
            JSONTokener jsonParser = new JSONTokener(strResult);
            JSONObject js = (JSONObject) jsonParser.nextValue();
            if (Integer.parseInt(js.getString("Code")) == 0) {
                JSONTokener jsonParser1 = new JSONTokener(strResult);
                JSONObject js1 = (JSONObject) jsonParser1.nextValue();
                if (js1.getString("Data") != null && !"".equals(js1.getString("Data"))) {
                    intTotalNum = Integer.parseInt(String.valueOf(ConvertJsonToList.getMap(js1.getString("Data")).get(
                            "Count")));
                    // 记录总条数
                    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
                    list = ConvertJsonToList.getList(String.valueOf(ConvertJsonToList.getMap(js1.getString("Data"))
                            .get("item")));
                    netDataList = new ArrayList<Map<String, Object>>(list);
                } else {
                    intTotalNum = 0;
                    if (netDataList != null) {
                        netDataList.clear();
                    }
                }
                showHList();
            } else {
                hemai_lv_tip.setVisibility(View.VISIBLE);
                hemai_lv_tip_tv.setText(js.getString("Data"));
            }
        } catch (JSONException e) {
            // 异常处理代码
            Log.e("error", e.toString());
        }
    }

    /**
     * 放入数据，显示listview视图
     */
    public void showHList() {
        int maxPage = (intTotalNum % PageSize == 0) ? (intTotalNum / PageSize) : (intTotalNum / PageSize + 1);
        // Log.e("maxPage", maxPage + "");
        if (PageNo < maxPage) {
            nextPage = true;
        } else {
            nextPage = false;
        }
        // Log.e("nextPage= ", nextPage + "");
        // //获取更多数据之外的请求，如首次请求数据，刷新，点击进度，战绩，总额
        if (!isPressMore) {
            mDataList = netDataList;
            // 填充UI,listview
            if (mDataList != null) {
                hemaiAdapter = new HemaiMainProcessListAdapter(getActivity(), mDataList);
                mListView.setAdapter(hemaiAdapter);
            }

        } else {
            // 获取更多数据
            // mDataList.addAll(HemaiDateUtil.SortListData(netDataList,
            // intOrderType, intOrderField));
            if(netDataList != null){
                mDataList.addAll(netDataList);
                hemaiAdapter.setData(mDataList);
                hemaiAdapter.notifyDataSetChanged();
            }
        }
        if (mDataList == null) {
            hemai_lv_tip.setVisibility(View.VISIBLE);
            hemai_lv_tip_tv.setText("无数据");
        }
        // 判断是否有下一页
        if (nextPage) {
        } else {
        }
        isPressMore = false;
    }



    //
//	@Override
    public void onRefresh() {
        // TODO Auto-generated method stub
        // System.out.println("=============onRefresh()=============");
        // 发送刷新请求
        PageNo = 1;
        startHBuyThread(lotID, PageNo);
    }
}
